package MetaInit

import (
	"database/sql"
	"errors"
	"fmt"
	"db2s/Er"
	"db2s/arg"
	"db2s/global"
	"reflect"
	"strings"
)

func getMetaInfoEr(dbType string) (res Er.TablesMetaInfoEr, err error) {
	if res, err = Er.MetaInfo(Er.TableInfoMeta{
		DBType: dbType,
	}); err != nil {
		//if WriteLog != nil {
		//	WriteLog.Error(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v failed. error is %v", event, table, schema, err))
		//}
		return
	}
	return
}
func getTablesMetaInfoInput(db map[string]*sql.DB, schema, table string, input global.StartPartConfigInputP) global.TablesMetaInfoInput {
	return global.TablesMetaInfoInput{
		DB: db,
		TableInfo: global.TableInfo{
			Schema: schema,
			Table:  table,
		},
		Input: input,
	}
}
func getTableExist(o Er.TablesMetaInfoEr, input global.TablesMetaInfoInput) (res any, err error) {
	if res, err = o.TableExist(input); err != nil {
		return
	}
	return
}
func getJobExist(o Er.TablesMetaInfoEr, input global.TablesMetaInfoInput) (res any, err error) {
	if res, err = o.JobExist(input); err != nil {
		return
	}
	if !strings.EqualFold(fmt.Sprintf("%v", reflect.TypeOf(res)), "global.ObjectExistReturnResult") {
		err = errors.New(fmt.Sprintf("type not match"))
		return
	}
	return
}
func getViewExist(o Er.TablesMetaInfoEr, input global.TablesMetaInfoInput) (res any, err error) {
	if res, err = o.ViewExist(input); err != nil {
		return
	}
	if !strings.EqualFold(fmt.Sprintf("%v", reflect.TypeOf(res)), "global.ObjectExistReturnResult") {
		err = errors.New(fmt.Sprintf("type not match"))
		return
	}
	return
}
func getSeqExist(o Er.TablesMetaInfoEr, input global.TablesMetaInfoInput) (res any, err error) {
	if res, err = o.SeqExist(input); err != nil {
		return
	}
	if !strings.EqualFold(fmt.Sprintf("%v", reflect.TypeOf(res)), "global.ObjectExistReturnResult") {
		err = errors.New(fmt.Sprintf("type not match"))
		return
	}
	return
}
func getTriggerExist(o Er.TablesMetaInfoEr, input global.TablesMetaInfoInput) (res any, err error) {
	if res, err = o.TriggerExist(input); err != nil {
		return
	}
	if !strings.EqualFold(fmt.Sprintf("%v", reflect.TypeOf(res)), "global.ObjectExistReturnResult") {
		err = errors.New(fmt.Sprintf("type not match"))
		return
	}
	return
}
func getForeignExist(o Er.TablesMetaInfoEr, input global.TablesMetaInfoInput) (res any, err error) {
	if res, err = o.ForeignExist(input); err != nil {
		return
	}
	if !strings.EqualFold(fmt.Sprintf("%v", reflect.TypeOf(res)), "global.ObjectExistReturnResult") {
		err = errors.New(fmt.Sprintf("type not match"))
		return
	}
	return
}
func getCustomizeTypeExist(o Er.TablesMetaInfoEr, input global.TablesMetaInfoInput) (res any, err error) {
	if res, err = o.CustomizeTypeExist(input); err != nil {
		return
	}
	if !strings.EqualFold(fmt.Sprintf("%v", reflect.TypeOf(res)), "global.ObjectExistReturnResult") {
		err = errors.New(fmt.Sprintf("type not match"))
		return
	}
	return
}
func getFuncExist(o Er.TablesMetaInfoEr, input global.TablesMetaInfoInput) (res any, err error) {
	if res, err = o.FuncExist(input); err != nil {
		return
	}
	if !strings.EqualFold(fmt.Sprintf("%v", reflect.TypeOf(res)), "global.ObjectExistReturnResult") {
		err = errors.New(fmt.Sprintf("type not match"))
		return
	}
	return
}
func getProcedureExist(o Er.TablesMetaInfoEr, input global.TablesMetaInfoInput) (res any, err error) {
	if res, err = o.ProcedureExist(input); err != nil {
		return
	}
	if !strings.EqualFold(fmt.Sprintf("%v", reflect.TypeOf(res)), "global.ObjectExistReturnResult") {
		err = errors.New(fmt.Sprintf("type not match"))
		return
	}
	return
}
func getPackageExist(o Er.TablesMetaInfoEr, input global.TablesMetaInfoInput) (res any, err error) {
	if res, err = o.PackageExist(input); err != nil {
		return
	}
	if !strings.EqualFold(fmt.Sprintf("%v", reflect.TypeOf(res)), "global.ObjectExistReturnResult") {
		err = errors.New(fmt.Sprintf("type not match"))
		return
	}
	return
}

// 获取当前数据库信息列表
func (t TableFilter) getSchema(db map[string]*sql.DB, s string, schema, table string, static global.StartPartConfigInputP) (result bool, err error) {
	var (
		resultAny any
		//event     = "[getSchema]"
		o Er.TablesMetaInfoEr
	)
	if o, err = getMetaInfoEr(s); err != nil {
		return
	}
	if resultAny, err = getTableExist(o, getTablesMetaInfoInput(db, schema, table, static)); err != nil {
		return
	}
	return resultAny.(global.ObjectExistReturnResult).Result, err
}
func (t TableFilter) getJob(db map[string]*sql.DB, s string, schema, table string, static global.StartPartConfigInputP) (result bool, err error) {
	var (
		resultAny any
		o         Er.TablesMetaInfoEr
	)
	if o, err = getMetaInfoEr(s); err != nil {
		return
	}
	if resultAny, err = getJobExist(o, getTablesMetaInfoInput(db, schema, table, static)); err != nil {
		return
	}
	return resultAny.(global.ObjectExistReturnResult).Result, err
}
func (t TableFilter) getView(db map[string]*sql.DB, s string, schema, table string, static global.StartPartConfigInputP) (result bool, err error) {
	var (
		resultAny any
		o         Er.TablesMetaInfoEr
	)
	if o, err = getMetaInfoEr(s); err != nil {
		return
	}
	if resultAny, err = getViewExist(o, getTablesMetaInfoInput(db, schema, table, static)); err != nil {
		return
	}
	return resultAny.(global.ObjectExistReturnResult).Result, err
}
func (t TableFilter) getSeq(db map[string]*sql.DB, s string, schema, table string, static global.StartPartConfigInputP) (result bool, err error) {
	var (
		resultAny any
		o         Er.TablesMetaInfoEr
	)
	if o, err = getMetaInfoEr(s); err != nil {
		return
	}
	if resultAny, err = getSeqExist(o, getTablesMetaInfoInput(db, schema, table, static)); err != nil {
		return
	}
	return resultAny.(global.ObjectExistReturnResult).Result, err
}
func (t TableFilter) getTrigger(db map[string]*sql.DB, s string, schema, table string, static global.StartPartConfigInputP) (result bool, err error) {
	var (
		resultAny any
		o         Er.TablesMetaInfoEr
	)
	if o, err = getMetaInfoEr(s); err != nil {
		return
	}
	if resultAny, err = getTriggerExist(o, getTablesMetaInfoInput(db, schema, table, static)); err != nil {
		return
	}
	return resultAny.(global.ObjectExistReturnResult).Result, err
}
func (t TableFilter) getForeign(db map[string]*sql.DB, s string, schema, table string, static global.StartPartConfigInputP) (result bool, err error) {
	var (
		resultAny any
		o         Er.TablesMetaInfoEr
	)
	if o, err = getMetaInfoEr(s); err != nil {
		return
	}
	if resultAny, err = getForeignExist(o, getTablesMetaInfoInput(db, schema, table, static)); err != nil {
		return
	}
	return resultAny.(global.ObjectExistReturnResult).Result, err
}
func (t TableFilter) getCustomizeType(db map[string]*sql.DB, s string, schema, table string, static global.StartPartConfigInputP) (result bool, err error) {
	var (
		resultAny any
		o         Er.TablesMetaInfoEr
	)
	if o, err = getMetaInfoEr(s); err != nil {
		return
	}
	if resultAny, err = getCustomizeTypeExist(o, getTablesMetaInfoInput(db, schema, table, static)); err != nil {
		return
	}
	return resultAny.(global.ObjectExistReturnResult).Result, err
}
func (t TableFilter) getFunc(db map[string]*sql.DB, s string, schema, table string, static global.StartPartConfigInputP) (result bool, err error) {
	var (
		resultAny any
		o         Er.TablesMetaInfoEr
	)
	if o, err = getMetaInfoEr(s); err != nil {
		return
	}
	if resultAny, err = getFuncExist(o, getTablesMetaInfoInput(db, schema, table, static)); err != nil {
		return
	}
	return resultAny.(global.ObjectExistReturnResult).Result, err
}

func (t TableFilter) getProcedure(db map[string]*sql.DB, s string, schema, table string, static global.StartPartConfigInputP) (result bool, err error) {
	var (
		resultAny any
		o         Er.TablesMetaInfoEr
	)
	if o, err = getMetaInfoEr(s); err != nil {
		return
	}
	if resultAny, err = getProcedureExist(o, getTablesMetaInfoInput(db, schema, table, static)); err != nil {
		return
	}
	return resultAny.(global.ObjectExistReturnResult).Result, err
}

func (t TableFilter) getPackage(db map[string]*sql.DB, s string, schema, table string, static global.StartPartConfigInputP) (result bool, err error) {
	var (
		resultAny any
		o         Er.TablesMetaInfoEr
	)
	if o, err = getMetaInfoEr(s); err != nil {
		return
	}
	if resultAny, err = getPackageExist(o, getTablesMetaInfoInput(db, schema, table, static)); err != nil {
		return
	}
	return resultAny.(global.ObjectExistReturnResult).Result, err
}

func (t TableFilter) getSrcSchema(event string, v arg.InputTableAttributes) (result bool, err error) {
	if result, err = t.getSchema(t.SDB, t.SourceDrive, v.Schema, v.Table, v.Static); err != nil {
		if WriteLog != nil {
			WriteLog.Error(fmt.Sprintf("(%v) %v Failed to obtain table %v.%v information from source. error info: %v", t.LogSeq, event, v.Schema, v.Table, err))
		}
		return
	}
	return
}
func (t TableFilter) getDstSchema(event string, v arg.InputTableAttributes) (result bool, err error) {
	if result, err = t.getSchema(t.DDB, t.DestDrive, v.MSchema, v.MTable, v.Static); err != nil {
		errorWriteInfo(fmt.Sprintf("(%v) %v Failed to obtain table %v.%v information from dest. error info: %v", t.LogSeq, event, v.Schema, v.Table, err))
		return
	}
	return
}

func (t TableFilter) getSrcJob(event string, v arg.InputTableAttributes) (result bool, err error) {
	if result, err = t.getJob(t.SDB, t.SourceDrive, v.Schema, v.Table, v.Static); err != nil {
		if WriteLog != nil {
			WriteLog.Error(fmt.Sprintf("(%v) %v Failed to obtain table %v.%v information from source. error info: %v", t.LogSeq, event, v.Schema, v.Table, err))
		}
		return
	}
	return
}
func (t TableFilter) getDstJob(event string, v arg.InputTableAttributes) (result bool, err error) {
	if result, err = t.getJob(t.DDB, t.DestDrive, v.MSchema, v.MTable, v.Static); err != nil {
		if WriteLog != nil {
			WriteLog.Error(fmt.Sprintf("(%v) %v Failed to obtain table %v.%v information from dest. error info: %v", t.LogSeq, event, v.Schema, v.Table, err))
		}
		return
	}
	return
}
func (t TableFilter) getSrcView(event string, v arg.InputTableAttributes) (result bool, err error) {
	if result, err = t.getView(t.SDB, t.SourceDrive, v.Schema, v.Table, v.Static); err != nil {
		if WriteLog != nil {
			WriteLog.Error(fmt.Sprintf("(%v) %v Failed to obtain table %v.%v information from source. error info: %v", t.LogSeq, event, v.Schema, v.Table, err))
		}
		return
	}
	return
}
func (t TableFilter) getDstView(event string, v arg.InputTableAttributes) (result bool, err error) {
	if result, err = t.getView(t.DDB, t.DestDrive, v.MSchema, v.MTable, v.Static); err != nil {
		if WriteLog != nil {
			WriteLog.Error(fmt.Sprintf("(%v) %v Failed to obtain table %v.%v information from dest. error info: %v", t.LogSeq, event, v.Schema, v.Table, err))
		}
		return
	}
	return
}

func (t TableFilter) getSrcSeq(event string, v arg.InputTableAttributes) (result bool, err error) {
	if result, err = t.getSeq(t.SDB, t.SourceDrive, v.Schema, v.Table, v.Static); err != nil {
		if WriteLog != nil {
			WriteLog.Error(fmt.Sprintf("(%v) %v Failed to obtain seq %v.%v information from source. error info: %v", t.LogSeq, event, v.Schema, v.Table, err))
		}
		return
	}
	return
}
func (t TableFilter) getDstSeq(event string, v arg.InputTableAttributes) (result bool, err error) {
	if result, err = t.getSeq(t.DDB, t.DestDrive, v.MSchema, v.MTable, v.Static); err != nil {
		if WriteLog != nil {
			WriteLog.Error(fmt.Sprintf("(%v) %v Failed to obtain seq %v.%v information from dest. error info: %v", t.LogSeq, event, v.Schema, v.Table, err))
		}
		return
	}
	return
}

func (t TableFilter) getSrcTrigger(event string, v arg.InputTableAttributes) (result bool, err error) {
	if result, err = t.getTrigger(t.SDB, t.SourceDrive, v.Schema, v.Table, v.Static); err != nil {
		if WriteLog != nil {
			WriteLog.Error(fmt.Sprintf("(%v) %v Failed to obtain trigger %v.%v information from source. error info: %v", t.LogSeq, event, v.Schema, v.Table, err))
		}
		return
	}
	return
}
func (t TableFilter) getDstTrigger(event string, v arg.InputTableAttributes) (result bool, err error) {
	if result, err = t.getTrigger(t.DDB, t.DestDrive, v.MSchema, v.MTable, v.Static); err != nil {
		if WriteLog != nil {
			WriteLog.Error(fmt.Sprintf("(%v) %v Failed to obtain trigger %v.%v information from dest. error info: %v", t.LogSeq, event, v.Schema, v.Table, err))
		}
		return
	}
	return
}
func (t TableFilter) getSrcForeign(event string, v arg.InputTableAttributes) (result bool, err error) {
	if result, err = t.getForeign(t.SDB, t.SourceDrive, v.Schema, v.Table, v.Static); err != nil {
		if WriteLog != nil {
			WriteLog.Error(fmt.Sprintf("(%v) %v Failed to obtain trigger %v.%v information from source. error info: %v", t.LogSeq, event, v.Schema, v.Table, err))
		}
		return
	}
	return
}
func (t TableFilter) getDstForeign(event string, v arg.InputTableAttributes) (result bool, err error) {
	if result, err = t.getForeign(t.DDB, t.DestDrive, v.MSchema, v.MTable, v.Static); err != nil {
		if WriteLog != nil {
			WriteLog.Error(fmt.Sprintf("(%v) %v Failed to obtain trigger %v.%v information from dest. error info: %v", t.LogSeq, event, v.Schema, v.Table, err))
		}
		return
	}
	return
}
func (t TableFilter) getSrcCustomizeType(event string, v arg.InputTableAttributes) (result bool, err error) {
	if result, err = t.getCustomizeType(t.SDB, t.SourceDrive, v.Schema, v.Table, v.Static); err != nil {
		if WriteLog != nil {
			WriteLog.Error(fmt.Sprintf("(%v) %v Failed to obtain trigger %v.%v information from source. error info: %v", t.LogSeq, event, v.Schema, v.Table, err))
		}
		return
	}
	return
}
func (t TableFilter) getDstCustomizeType(event string, v arg.InputTableAttributes) (result bool, err error) {
	if result, err = t.getCustomizeType(t.DDB, t.DestDrive, v.MSchema, v.MTable, v.Static); err != nil {
		if WriteLog != nil {
			WriteLog.Error(fmt.Sprintf("(%v) %v Failed to obtain trigger %v.%v information from dest. error info: %v", t.LogSeq, event, v.Schema, v.Table, err))
		}
		return
	}
	return
}
func (t TableFilter) getSrcFunc(event string, v arg.InputTableAttributes) (result bool, err error) {
	if result, err = t.getFunc(t.SDB, t.SourceDrive, v.Schema, v.Table, v.Static); err != nil {
		if WriteLog != nil {
			WriteLog.Error(fmt.Sprintf("(%v) %v Failed to obtain trigger %v.%v information from source. error info: %v", t.LogSeq, event, v.Schema, v.Table, err))
		}
		return
	}
	return
}
func (t TableFilter) getDstFunc(event string, v arg.InputTableAttributes) (result bool, err error) {
	if result, err = t.getFunc(t.DDB, t.DestDrive, v.MSchema, v.MTable, v.Static); err != nil {
		if WriteLog != nil {
			WriteLog.Error(fmt.Sprintf("(%v) %v Failed to obtain trigger %v.%v information from dest. error info: %v", t.LogSeq, event, v.Schema, v.Table, err))
		}
		return
	}
	return
}

func (t TableFilter) getSrcProcedure(event string, v arg.InputTableAttributes) (result bool, err error) {
	if result, err = t.getProcedure(t.SDB, t.SourceDrive, v.Schema, v.Table, v.Static); err != nil {
		if WriteLog != nil {
			WriteLog.Error(fmt.Sprintf("(%v) %v Failed to obtain trigger %v.%v information from source. error info: %v", t.LogSeq, event, v.Schema, v.Table, err))
		}
		return
	}
	return
}
func (t TableFilter) getDstProcedure(event string, v arg.InputTableAttributes) (result bool, err error) {
	if result, err = t.getProcedure(t.DDB, t.DestDrive, v.MSchema, v.MTable, v.Static); err != nil {
		if WriteLog != nil {
			WriteLog.Error(fmt.Sprintf("(%v) %v Failed to obtain trigger %v.%v information from dest. error info: %v", t.LogSeq, event, v.Schema, v.Table, err))
		}
		return
	}
	return
}
func (t TableFilter) getSrcPackage(event string, v arg.InputTableAttributes) (result bool, err error) {
	if result, err = t.getPackage(t.SDB, t.SourceDrive, v.Schema, v.Table, v.Static); err != nil {
		if WriteLog != nil {
			WriteLog.Error(fmt.Sprintf("(%v) %v Failed to obtain trigger %v.%v information from source. error info: %v", t.LogSeq, event, v.Schema, v.Table, err))
		}
		return
	}
	return
}
func (t TableFilter) getDstPackage(event string, v arg.InputTableAttributes) (result bool, err error) {
	if result, err = t.getPackage(t.DDB, t.DestDrive, v.MSchema, v.MTable, v.Static); err != nil {
		if WriteLog != nil {
			WriteLog.Error(fmt.Sprintf("(%v) %v Failed to obtain trigger %v.%v information from dest. error info: %v", t.LogSeq, event, v.Schema, v.Table, err))
		}
		return
	}
	return
}

// GetTableList 获取当前数据库信息列表
func (t TableFilter) GetTableList(db map[string]*sql.DB, s string, schema, table string, static global.StartPartConfigInputP) (result []string, err error) {
	var (
		resultAny global.ObjectNameReturnResult
		event     = "[GetTableList]"
		o         Er.TablesMetaInfoEr
	)
	if o, err = getMetaInfoEr(s); err != nil {
		return
	}
	if resultAny, err = o.TableName(getTablesMetaInfoInput(db, schema, table, static)); err != nil {
		errorWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v failed. exec sql is %v,error is %v", event, table, schema, resultAny.BaseResult.Sql, err))
		return
	}
	if resultAny.Result == nil {
		warnWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v is empty. exec sql is %v", event, table, schema, resultAny.BaseResult.Sql))
		return
	}
	result = resultAny.Result
	infoWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v. exec sql is %v,exec time is %vms", event, table, schema, resultAny.BaseResult.Sql, resultAny.BaseResult.TimeOut))
	debugWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v. exec sql is %v,exec time is %vms,return result is %v", event, table, schema, resultAny.BaseResult.Sql, resultAny.BaseResult.TimeOut, resultAny.Result))
	return
}

// GetJobList 获取当前数据库信息列表
func (t TableFilter) GetJobList(db map[string]*sql.DB, s string, schema, table string, static global.StartPartConfigInputP) (result []string, err error) {
	var (
		resultAny global.ObjectNameReturnResult
		event     = "[GetTableList]"
		o         Er.TablesMetaInfoEr
	)
	if o, err = getMetaInfoEr(s); err != nil {
		return
	}
	if resultAny, err = o.JobName(getTablesMetaInfoInput(db, schema, table, static)); err != nil {
		errorWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v failed. exec sql is %v,error is %v", event, table, schema, resultAny.BaseResult.Sql, err))
		return
	}
	if resultAny.Result == nil {
		warnWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v is empty. exec sql is %v", event, table, schema, resultAny.BaseResult.Sql))
		return
	}
	result = resultAny.Result
	infoWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v. exec sql is %v,exec time is %vms", event, table, schema, resultAny.BaseResult.Sql, resultAny.BaseResult.TimeOut))
	debugWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v. exec sql is %v,exec time is %vms,return result is %v", event, table, schema, resultAny.BaseResult.Sql, resultAny.BaseResult.TimeOut, resultAny.Result))
	return
}

// GetViewList 获取当前数据库信息列表
func (t TableFilter) GetViewList(db map[string]*sql.DB, s string, schema, table string, static global.StartPartConfigInputP) (result []string, err error) {
	var (
		resultAny global.ObjectNameReturnResult
		event     = "[GetViewList]"
		o         Er.TablesMetaInfoEr
	)
	if o, err = getMetaInfoEr(s); err != nil {
		return
	}
	if resultAny, err = o.ViewName(getTablesMetaInfoInput(db, schema, table, static)); err != nil {
		errorWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v failed. exec sql is %v,error is %v", event, table, schema, resultAny.BaseResult.Sql, err))
		return
	}
	if resultAny.Result == nil {
		errorWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v is empty. exec sql is %v", event, table, schema, resultAny.BaseResult.Sql))
		return
	}
	result = resultAny.Result
	infoWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v. exec sql is %v,exec time is %vms", event, table, schema, resultAny.BaseResult.Sql, resultAny.BaseResult.TimeOut))
	debugWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v. exec sql is %v,exec time is %vms,return result is %v", event, table, schema, resultAny.BaseResult.Sql, resultAny.BaseResult.TimeOut, resultAny.Result))
	return
}

// GetSeqList 获取当前数据库信息列表
func (t TableFilter) GetSeqList(db map[string]*sql.DB, s string, schema, table string, static global.StartPartConfigInputP) (result []string, err error) {
	var (
		resultAny global.ObjectNameReturnResult
		event     = "[GetViewList]"
		o         Er.TablesMetaInfoEr
	)
	if o, err = getMetaInfoEr(s); err != nil {
		return
	}
	if resultAny, err = o.SeqName(getTablesMetaInfoInput(db, schema, table, static)); err != nil {
		errorWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v failed. exec sql is %v,error is %v", event, table, schema, resultAny.BaseResult.Sql, err))
		return
	}
	if resultAny.Result == nil {
		errorWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v is empty. exec sql is %v", event, table, schema, resultAny.BaseResult.Sql))
		return
	}
	result = resultAny.Result
	infoWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v. exec sql is %v,exec time is %vms", event, table, schema, resultAny.BaseResult.Sql, resultAny.BaseResult.TimeOut))
	debugWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v. exec sql is %v,exec time is %vms,return result is %v", event, table, schema, resultAny.BaseResult.Sql, resultAny.BaseResult.TimeOut, resultAny.Result))
	return
}

// GetTriggerList 获取当前数据库信息列表
func (t TableFilter) GetTriggerList(db map[string]*sql.DB, s string, schema, table string, static global.StartPartConfigInputP) (result []string, err error) {
	var (
		resultAny global.ObjectNameReturnResult
		event     = "[GetViewList]"
		o         Er.TablesMetaInfoEr
	)
	if o, err = getMetaInfoEr(s); err != nil {
		return
	}
	if resultAny, err = o.TriggerName(getTablesMetaInfoInput(db, schema, table, static)); err != nil {
		errorWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v failed. exec sql is %v,error is %v", event, table, schema, resultAny.BaseResult.Sql, err))
		return
	}
	if resultAny.Result == nil {
		errorWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v is empty. exec sql is %v", event, table, schema, resultAny.BaseResult.Sql))
		return
	}
	result = resultAny.Result
	infoWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v. exec sql is %v,exec time is %vms", event, table, schema, resultAny.BaseResult.Sql, resultAny.BaseResult.TimeOut))
	debugWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v. exec sql is %v,exec time is %vms,return result is %v", event, table, schema, resultAny.BaseResult.Sql, resultAny.BaseResult.TimeOut, resultAny.Result))
	return
}

// GetForeignList 获取当前数据库信息列表
func (t TableFilter) GetForeignList(db map[string]*sql.DB, s string, schema, table string, static global.StartPartConfigInputP) (result []string, err error) {
	var (
		resultAny global.ObjectNameReturnResult
		event     = "[GetViewList]"
		o         Er.TablesMetaInfoEr
	)
	if o, err = getMetaInfoEr(s); err != nil {
		return
	}
	if resultAny, err = o.ForeignName(getTablesMetaInfoInput(db, schema, table, static)); err != nil {
		errorWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v failed. exec sql is %v,error is %v", event, table, schema, resultAny.BaseResult.Sql, err))
		return
	}
	if resultAny.Result == nil {
		errorWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v is empty. exec sql is %v", event, table, schema, resultAny.BaseResult.Sql))
		return
	}
	result = resultAny.Result
	infoWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v. exec sql is %v,exec time is %vms", event, table, schema, resultAny.BaseResult.Sql, resultAny.BaseResult.TimeOut))
	debugWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v. exec sql is %v,exec time is %vms,return result is %v", event, table, schema, resultAny.BaseResult.Sql, resultAny.BaseResult.TimeOut, resultAny.Result))
	return
}

// GetCustomizeTypeList 获取当前数据库信息列表
func (t TableFilter) GetCustomizeTypeList(db map[string]*sql.DB, s string, schema, table string, static global.StartPartConfigInputP) (result []string, err error) {
	var (
		resultAny global.ObjectNameReturnResult
		event     = "[GetViewList]"
		o         Er.TablesMetaInfoEr
	)
	if o, err = getMetaInfoEr(s); err != nil {
		return
	}
	if resultAny, err = o.CustomizeTypeName(getTablesMetaInfoInput(db, schema, table, static)); err != nil {
		errorWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v failed. exec sql is %v,error is %v", event, table, schema, resultAny.BaseResult.Sql, err))
		return
	}
	if resultAny.Result == nil {
		errorWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v is empty. exec sql is %v", event, table, schema, resultAny.BaseResult.Sql))
		return
	}
	result = resultAny.Result
	infoWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v. exec sql is %v,exec time is %vms", event, table, schema, resultAny.BaseResult.Sql, resultAny.BaseResult.TimeOut))
	debugWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v. exec sql is %v,exec time is %vms,return result is %v", event, table, schema, resultAny.BaseResult.Sql, resultAny.BaseResult.TimeOut, resultAny.Result))
	return
}

// GetFuncList 获取当前数据库信息列表
func (t TableFilter) GetFuncList(db map[string]*sql.DB, s string, schema, table string, static global.StartPartConfigInputP) (result []string, err error) {
	var (
		resultAny global.ObjectNameReturnResult
		event     = "[GetViewList]"
		o         Er.TablesMetaInfoEr
	)
	if o, err = getMetaInfoEr(s); err != nil {
		return
	}
	if resultAny, err = o.FuncName(getTablesMetaInfoInput(db, schema, table, static)); err != nil {
		errorWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v failed. exec sql is %v,error is %v", event, table, schema, resultAny.BaseResult.Sql, err))
		return
	}
	if resultAny.Result == nil {
		errorWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v is empty. exec sql is %v", event, table, schema, resultAny.BaseResult.Sql))
		return
	}
	result = resultAny.Result
	infoWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v. exec sql is %v,exec time is %vms", event, table, schema, resultAny.BaseResult.Sql, resultAny.BaseResult.TimeOut))
	debugWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v. exec sql is %v,exec time is %vms,return result is %v", event, table, schema, resultAny.BaseResult.Sql, resultAny.BaseResult.TimeOut, resultAny.Result))
	return
}

// GetProcedureList 获取当前数据库信息列表
func (t TableFilter) GetProcedureList(db map[string]*sql.DB, s string, schema, table string, static global.StartPartConfigInputP) (result []string, err error) {
	var (
		resultAny global.ObjectNameReturnResult
		event     = "[GetViewList]"
		o         Er.TablesMetaInfoEr
	)
	if o, err = getMetaInfoEr(s); err != nil {
		return
	}
	if resultAny, err = o.ProcedureName(getTablesMetaInfoInput(db, schema, table, static)); err != nil {
		errorWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v failed. exec sql is %v,error is %v", event, table, schema, resultAny.BaseResult.Sql, err))
		return
	}
	if resultAny.Result == nil {
		errorWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v is empty. exec sql is %v", event, table, schema, resultAny.BaseResult.Sql))
		return
	}
	result = resultAny.Result
	infoWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v. exec sql is %v,exec time is %vms", event, table, schema, resultAny.BaseResult.Sql, resultAny.BaseResult.TimeOut))
	debugWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v. exec sql is %v,exec time is %vms,return result is %v", event, table, schema, resultAny.BaseResult.Sql, resultAny.BaseResult.TimeOut, resultAny.Result))
	return
}

// GetPackageList 获取当前数据库信息列表
func (t TableFilter) GetPackageList(db map[string]*sql.DB, s string, schema, table string, static global.StartPartConfigInputP) (result []string, err error) {
	var (
		resultAny global.ObjectNameReturnResult
		event     = "[GetViewList]"
		o         Er.TablesMetaInfoEr
	)
	if o, err = getMetaInfoEr(s); err != nil {
		return
	}
	if resultAny, err = o.PackageName(getTablesMetaInfoInput(db, schema, table, static)); err != nil {
		errorWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v failed. exec sql is %v,error is %v", event, table, schema, resultAny.BaseResult.Sql, err))
		return
	}
	if resultAny.Result == nil {
		errorWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v is empty. exec sql is %v", event, table, schema, resultAny.BaseResult.Sql))
		return
	}
	result = resultAny.Result
	infoWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v. exec sql is %v,exec time is %vms", event, table, schema, resultAny.BaseResult.Sql, resultAny.BaseResult.TimeOut))
	debugWriteInfo(fmt.Sprintf("%v Get the table wildcard %v under the specified schema %v. exec sql is %v,exec time is %vms,return result is %v", event, table, schema, resultAny.BaseResult.Sql, resultAny.BaseResult.TimeOut, resultAny.Result))
	return
}
func (t TableFilter) getSchemaObjectList() map[string]reflect.Value {
	return map[string]reflect.Value{
		"table":     reflect.ValueOf(t.GetTableList),
		"job":       reflect.ValueOf(t.GetJobList),
		"view":      reflect.ValueOf(t.GetViewList),
		"seq":       reflect.ValueOf(t.GetSeqList),
		"trigger":   reflect.ValueOf(t.GetTriggerList),
		"foreign":   reflect.ValueOf(t.GetForeignList),
		"type":      reflect.ValueOf(t.GetCustomizeTypeList),
		"func":      reflect.ValueOf(t.GetFuncList),
		"procedure": reflect.ValueOf(t.GetProcedureList),
		"package":   reflect.ValueOf(t.GetPackageList),
	}
}
func (t TableFilter) getRoleFunc() map[string]reflect.Value {
	return map[string]reflect.Value{
		"table":     reflect.ValueOf(t.InitToSchema2Table),
		"job":       reflect.ValueOf(t.InitToSchema2Job),
		"view":      reflect.ValueOf(t.InitToSchema2View),
		"seq":       reflect.ValueOf(t.InitToSchema2Seq),
		"trigger":   reflect.ValueOf(t.InitToSchema2Trigger),
		"foreign":   reflect.ValueOf(t.InitToSchema2Foreign),
		"type":      reflect.ValueOf(t.InitToSchema2CustomizeType),
		"func":      reflect.ValueOf(t.InitToSchema2Func),
		"procedure": reflect.ValueOf(t.InitToSchema2Procedure),
		"package":   reflect.ValueOf(t.InitToSchema2Package),
	}
}

func (t TableFilter) InitToSchemaObjectList(db map[string]*sql.DB, s, schema, table, role string, static global.StartPartConfigInputP) (result []string, err error) {
	if Func, ok := t.getSchemaObjectList()[role]; ok {
		args := []reflect.Value{
			reflect.ValueOf(db),
			reflect.ValueOf(s),
			reflect.ValueOf(schema),
			reflect.ValueOf(table),
			reflect.ValueOf(static),
		}
		res := Func.Call(args)
		for x, y := range res {
			switch x {
			case 0:
				result = y.Interface().([]string)
			case 1:
				if y.Interface() != nil {
					err = y.Interface().(error)
				}
			}
		}
	}
	return
}
func (t TableFilter) InitToActiveObject(v arg.InputTableAttributes) (result bool, err error) {
	Func, ok := t.getRoleFunc()[v.TableAttributes.Role]
	if !ok {
		err = errors.New(fmt.Sprintf("role input error. role message is \"%v\"", v.TableAttributes.Role))
	}
	args := []reflect.Value{
		reflect.ValueOf(v),
	}
	res := Func.Call(args)
	for x, y := range res {
		switch x {
		case 0:
			result = y.Bool()
		case 1:
			if y.Interface() != nil {
				err = y.Interface().(error)
			}
		}
	}
	return
}
func (t TableFilter) InitToSchema2Job(v arg.InputTableAttributes) (result bool, err error) {
	var (
		sourceResult, destResult bool
		event                    = "[InitToSchema2Job]"
	)
	switch v.TableJoin {
	case "left":
		return t.getSrcJob(event, v)
	case "right":
		return t.getDstJob(event, v)
	case "join":
		if sourceResult, err = t.getSrcJob(event, v); err != nil {
			return
		}
		if destResult, err = t.getDstJob(event, v); err != nil {
			return
		}
		if sourceResult && destResult {
			return true, err
		}
		return false, err
	}
	return
}
func (t TableFilter) InitToSchema2View(v arg.InputTableAttributes) (result bool, err error) {
	var (
		sourceResult, destResult bool
		event                    = "[InitToSchema2View]"
	)
	switch v.TableJoin {
	case "left":
		return t.getSrcView(event, v)
	case "right":
		return t.getDstView(event, v)
	case "join":
		if sourceResult, err = t.getSrcView(event, v); err != nil {
			return
		}
		if destResult, err = t.getDstView(event, v); err != nil {
			return
		}
		if sourceResult && destResult {
			return true, err
		}
		return false, err
	}
	return
}

func (t TableFilter) InitToSchema2Seq(v arg.InputTableAttributes) (result bool, err error) {
	var (
		sourceResult, destResult bool
		event                    = "[InitToSchema2Seq]"
	)
	switch v.TableJoin {
	case "left":
		return t.getSrcSeq(event, v)
	case "right":
		return t.getDstSeq(event, v)
	case "join":
		if sourceResult, err = t.getSrcSeq(event, v); err != nil {
			return
		}
		if destResult, err = t.getDstSeq(event, v); err != nil {
			return
		}
		if sourceResult && destResult {
			return true, err
		}
		return false, err
	}
	return
}
func (t TableFilter) InitToSchema2Trigger(v arg.InputTableAttributes) (result bool, err error) {
	var (
		sourceResult, destResult bool
		event                    = "[InitToSchema2Trigger]"
	)
	switch v.TableJoin {
	case "left":
		return t.getSrcTrigger(event, v)
	case "right":
		return t.getDstTrigger(event, v)
	case "join":
		if sourceResult, err = t.getSrcTrigger(event, v); err != nil {
			return
		}
		if destResult, err = t.getDstTrigger(event, v); err != nil {
			return
		}
		if sourceResult && destResult {
			return true, err
		}
		return false, err
	}
	return
}
func (t TableFilter) InitToSchema2Foreign(v arg.InputTableAttributes) (result bool, err error) {
	var (
		sourceResult, destResult bool
		event                    = "[InitToSchema2Foreign]"
	)
	switch v.TableJoin {
	case "left":
		return t.getSrcForeign(event, v)
	case "right":
		return t.getDstForeign(event, v)
	case "join":
		if sourceResult, err = t.getSrcForeign(event, v); err != nil {
			return
		}
		if destResult, err = t.getDstForeign(event, v); err != nil {
			return
		}
		if sourceResult && destResult {
			return true, err
		}
		return false, err
	}
	return
}
func (t TableFilter) InitToSchema2CustomizeType(v arg.InputTableAttributes) (result bool, err error) {
	var (
		sourceResult, destResult bool
		event                    = "[InitToSchema2CustomizeType]"
	)
	switch v.TableJoin {
	case "left":
		return t.getSrcCustomizeType(event, v)
	case "right":
		return t.getDstCustomizeType(event, v)
	case "join":
		if sourceResult, err = t.getSrcCustomizeType(event, v); err != nil {
			return
		}
		if destResult, err = t.getDstCustomizeType(event, v); err != nil {
			return
		}
		if sourceResult && destResult {
			return true, err
		}
		return false, err
	}
	return
}
func (t TableFilter) InitToSchema2Func(v arg.InputTableAttributes) (result bool, err error) {
	var (
		sourceResult, destResult bool
		event                    = "[InitToSchema2Func]"
	)
	switch v.TableJoin {
	case "left":
		return t.getSrcFunc(event, v)
	case "right":
		return t.getDstFunc(event, v)
	case "join":
		if sourceResult, err = t.getSrcFunc(event, v); err != nil {
			return
		}
		if destResult, err = t.getDstFunc(event, v); err != nil {
			return
		}
		if sourceResult && destResult {
			return true, err
		}
		return false, err
	}
	return
}
func (t TableFilter) InitToSchema2Procedure(v arg.InputTableAttributes) (result bool, err error) {
	var (
		sourceResult, destResult bool
		event                    = "[InitToSchema2Procedure]"
	)
	switch v.TableJoin {
	case "left":
		return t.getSrcProcedure(event, v)
	case "right":
		return t.getDstProcedure(event, v)
	case "join":
		if sourceResult, err = t.getSrcProcedure(event, v); err != nil {
			return
		}
		if destResult, err = t.getDstProcedure(event, v); err != nil {
			return
		}
		if sourceResult && destResult {
			return true, err
		}
		return false, err
	}
	return
}

func (t TableFilter) InitToSchema2Package(v arg.InputTableAttributes) (result bool, err error) {
	var (
		sourceResult, destResult bool
		event                    = "[InitToSchema2Package]"
	)
	switch v.TableJoin {
	case "left":
		return t.getSrcPackage(event, v)
	case "right":
		return t.getDstPackage(event, v)
	case "join":
		if sourceResult, err = t.getSrcPackage(event, v); err != nil {
			return
		}
		if destResult, err = t.getDstPackage(event, v); err != nil {
			return
		}
		if sourceResult && destResult {
			return true, err
		}
		return false, err
	}
	return
}

// InitToSchema2Table 初始化 执行计划 以 map 的形式 key 表名 value SchemaCheck
func (t TableFilter) InitToSchema2Table(v arg.InputTableAttributes) (result bool, err error) {
	var (
		sourceResult, destResult bool
		event                    = "[InitToSchema2Table]"
	)
	switch v.TableJoin {
	case "left":
		return t.getSrcSchema(event, v)
	case "right":
		return t.getDstSchema(event, v)
	case "join":
		if sourceResult, err = t.getSrcSchema(event, v); err != nil {
			return
		}
		if destResult, err = t.getDstSchema(event, v); err != nil {
			return
		}
		if sourceResult && destResult {
			return true, err
		}
		return false, err
	}
	return
}
